package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.*;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 攻略资源控制器
 */
@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private ITagService tagService;


    /**
     * 查询所有攻略信息 分页
     *
     * @return
     */
    @GetMapping
    public PageInfo query(StrategyQueryObject qo) {
        return strategyService.query(qo);
    }

    /**
     * 根据大攻略id查询(单条)大攻略信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Strategy getBystrategyId(@PathVariable("id") Long id) {
        return strategyService.getBystrategyId(id);
    }

    /**
     * 根据大攻略id查询攻略分类目录信息(集合)
     *
     * @param id
     * @return
     */
    @GetMapping("{id}/catalogs")
    public List<StrategyCatalog> listByStrategyId(@PathVariable("id") Long id) {
        return strategyCatalogService.listByStrategyId(id);
    }

    /**
     * 查询文章内容
     *
     * @param id
     * @return
     */
    @GetMapping("details/{id}")
    public StrategyDetail getDetailById(@PathVariable("id") Long id) {
        return strategyDetailService.getDetailById(id);

    }

    /**
     * 根据大攻略id评论集合
     *
     * @param qo
     * @return
     */
    @GetMapping("{strategyId}/commends")
    public PageInfo queryCommends(StrategyCommentQueryObject qo) {
        qo.setOrderBy("sc.createTime desc");
        return strategyCommentService.query(qo);
    }


    /**
     * 添加某个攻略的评论
     *
     * @param strategyComment,tags
     * @return
     */
    @PostMapping("{strategy.id}/commends")
    public JSONResult queryCommends(StrategyComment strategyComment, String[] tags) {
        strategyCommentService.save(strategyComment, tags);
        return new JSONResult();
    }


    /**
     * 根据大攻略id热门标签集合
     *
     * @param strategyId
     * @return
     */
    @GetMapping("{strategyId}/tags")
    public List<Tag> listTags(@PathVariable("strategyId") Long strategyId) {
        return tagService.listTags(strategyId);
    }

    /**
     * 根据关键字查询大攻略
     *
     * @param qo
     * @return
     */
    @GetMapping("{keyword}/search")
    public PageInfo selectStrategyByKeyword(StrategyQueryObject qo) {
        //设置查询热门攻略
        qo.setState(Strategy.STATE_HOT);
        //判断值为1就查询攻略
        PageInfo<Strategy> query = strategyService.query(qo);
        return query;
    }

    /**
     * 根据关键字查询大攻略数据条数
     * @param qo
     * @return
     */
    @GetMapping("{keyword}/countNum")
    public int selectCountByKeyword(StrategyQueryObject qo){
        //设置查询热门攻略
        qo.setState(Strategy.STATE_HOT);
        int countNum=strategyService.selectCountByKeyword(qo);
        return countNum;
    }

}
